package com.zhaohui.controller;

import com.zhaohui.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class MyServlet extends HttpServlet {
    private UserService service = new UserService();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String acc = req.getParameter("acc");
        String pass = req.getParameter("pass");
        boolean boo = service.login(acc, pass);
        myExport(resp,boo,"登录成功！","登录失败");
    }

    private void myExport(HttpServletResponse resp, boolean boo, String s, String f) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        PrintWriter wr = resp.getWriter();
        if (boo){
            wr.write("<script>"+
                    "alert('"+s+"');"+
                    "location.href='login.html';"+
                    "</script>");
        }else {
            wr.write("<script>"+
                    "alert('"+f+"');"+
                    "location.href='login.html';"+
                    "</script>");
        }
        wr.close();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
